package com.qf.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qf.dao.TbComputerMapper;
import com.qf.dto.PageDto;
import com.qf.dto.TbComputerDto;
import com.qf.pojo.TbComputer;
import com.qf.pojo.TbComputerExample;
import com.qf.utils.DateUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;
@Service("tbComputerService")
public class TbComputerServiceImpl implements TbComputerService {
    @Autowired
    private TbComputerMapper tbComputerMapper;

    @Override
    public void addComputer(TbComputerDto dto) throws ParseException {
        TbComputer pojo=new TbComputer();
        BeanUtils.copyProperties(dto,pojo);
        //时间需要单独处理
        pojo.setComGendate(DateUtils.stringToDate(dto.getComGendate()));
        tbComputerMapper.insertSelective(pojo);
    }

    @Override
    public void delComputer(Long comId) {
        tbComputerMapper.deleteByPrimaryKey(comId);
    }

    @Override
    public void updateComputer(TbComputerDto dto) throws ParseException {
        TbComputer pojo=new TbComputer();
        BeanUtils.copyProperties(dto,pojo);
        pojo.setComGendate(DateUtils.stringToDate(dto.getComGendate()));
        tbComputerMapper.updateByPrimaryKeySelective(pojo);
    }

    @Override
    public TbComputerDto findById(Long comId) {
        TbComputer pojo=tbComputerMapper.selectByPrimaryKey(comId);
        TbComputerDto dto=new TbComputerDto();
        BeanUtils.copyProperties(pojo,dto);
        dto.setComGendate(DateUtils.dateToString(pojo.getComGendate()));
        return dto;
    }

    @Override
    public List<TbComputerDto> findByLike(String comModel) {
        List<TbComputerDto> list=new ArrayList<>();
        TbComputerExample example=new TbComputerExample();
        //添加模糊查询条件
        TbComputerExample.Criteria criteria=example.createCriteria();
        criteria.andComModelLike("%"+comModel+"%");
        List<TbComputer> pojolist=tbComputerMapper.selectByExample(example);
        for (TbComputer pojo : pojolist) {
            TbComputerDto dto=new TbComputerDto();
            BeanUtils.copyProperties(pojo,dto);
            list.add(dto);
        }
        return list;
    }

    @Override
    public List<TbComputerDto> findBypage(PageDto pageDto) {
        List<TbComputerDto> list=new ArrayList<>();
        //加入动态分页，在查询之前拦截
        PageHelper.startPage(pageDto.getCurrentPage(),pageDto.getSize());
        TbComputerExample example=new TbComputerExample();
        List<TbComputer> pojo=tbComputerMapper.selectByExample(example);
        PageInfo pageInfo=new PageInfo(pojo);
        //获取总记录数
        Long total=pageInfo.getTotal();
        //获取总页数
        int MaxPage=pageInfo.getPages();
        //返回参数
        pageDto.setMaxPage(MaxPage);
        for (TbComputer pojos : pojo) {
            TbComputerDto dto=new TbComputerDto();
            BeanUtils.copyProperties(pojos,dto);
            dto.setComGendate(DateUtils.dateToString(pojos.getComGendate()));
            list.add(dto);
        }
        return list;
    }
}
